const express = require("express")
const router = express.Router()
const { v4: uuidv4 } = require("uuid")//引入uuid
const { db, genid } = require("../db/DbUtils")

router.post("/login", async (req, res) => {
  let { account, password } = req.body
  let { err, rows } = await db.async.all("select * from `admin` where `account` = ? and `password` = ?", [account, password])
  if (err == null && rows.length > 0) {
    let login_token = uuidv4()
    await db.async.run("update `admin` set `token` = ? where `id` = ?", [login_token, rows[0].id])
    let admin_info = rows[0]
    admin_info.token = login_token
    admin_info.password = ""
    res.send({
      code: 200,
      msg: "登录成功",
      data: admin_info
    })
  } else {
    res.send({
      code: 500,
      msg: "登录失败"
    })
  }
})

module.exports = router